package cn.yikaojiuguo.student.ui.ask;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.adapter.ChooseDialogViewHolder;
import cn.yikaojiuguo.student.adapter.SimpleBaseAdapter;
import cn.yikaojiuguo.student.ui.base.BasePopupWindow;

/**
 * Created by cfan_yang on 2016/2/17.
 */
public class ChooseRefuseContentPopupWindow extends BasePopupWindow {

    private OnItemClickListener mListener;

    public ChooseRefuseContentPopupWindow(Context context, float widthPercent, float heightPercent) {
        super(context, widthPercent, heightPercent);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initView(Context context) {
        TextView tv_title = (TextView) mConvertView.findViewById(R.id.tv_title);
        ListView lv = (ListView) mConvertView.findViewById(R.id.lv);
        tv_title.setText("拒绝理由");
        RefuseAdapter adapter = new RefuseAdapter(context, Arrays.asList(context.getResources().getStringArray(R.array.refuse)));
        lv.setAdapter(adapter);
    }


    @Override
    protected int getLayoutResourceId() {
        return R.layout.layout_dialog_change;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(String content);
    }

    class RefuseAdapter extends SimpleBaseAdapter<String> {

        private int mCurIndex = 0;

        public RefuseAdapter(Context context, List<String> list) {
            super(context, list);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ChooseDialogViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_dialog_list_choose, null);
                holder = new ChooseDialogViewHolder();
                holder.tv_text = (TextView) convertView.findViewById(R.id.tv_text);
                holder.iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
                convertView.setTag(holder);
            } else {
                holder = (ChooseDialogViewHolder) convertView.getTag();
            }
            int visibility = position == mCurIndex?View.VISIBLE:View.INVISIBLE;
            holder.iv_choose.setVisibility(visibility);
            holder.tv_text.setText(getItem(position));
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCurIndex = position;
                    notifyDataSetChanged();
                    if(mListener!=null) {
                        mListener.onItemClick(getItem(position));
                    }
                }
            });
            return convertView;
        }
    }
}
